package tr.edu.ege.seagent.hobnobandroidapplication.activity;

import java.util.ArrayList;

import tr.edu.ege.seagent.hobnobandroidapplication.R;
import tr.edu.ege.seagent.hobnobandroidapplication.task.RetrieveNotificationsTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NotificationListActivity extends Activity {

	/**
	 * URL of servlet which returns the list of notifications in the response.
	 */
	public static final String NOTIFICATION_SERVLET_URL = "http://155.223.24.19:8080/HobNobWebApp/HobNobAndroidServlet";

	/**
	 * Android listview component.
	 */
	protected ListView mainListView;
	/**
	 * ArrayAdapter which is using for display notification list to the
	 * listview.
	 */
	protected ArrayAdapter<String> listAdapter;

	/**
	 * Class tag for logging.
	 */
	private static final String TAG = "NotificationListActivity";

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 * 
	 * 
	 * Called when the activity first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "Content view is setting.");
		setContentView(R.layout.activity_hob_nob_list_notification);
		// get response content on other thread...
		Log.i(TAG, "HttpAccess is executing.");

		// gets notification list from other thread...
		executeGetNotificationListTask();

	}

	private void handleRetrieveNotificationsTaskResult(
			ArrayList<String> notificationList) {
		// display notification list on the screen with list view
		displayNotificationList(notificationList);

		setClickListenerForListView(mainListView);
	}

	/**
	 * This function provide us to get notification list from AsyncTask.
	 */
	private void executeGetNotificationListTask() {
		new RetrieveNotificationsTask(this).execute(NOTIFICATION_SERVLET_URL);
	}

	/**
	 * This function provides us to display response's content as a ListView.
	 * 
	 * @param notificationList
	 *            list of notification resource URIs.
	 */
	private void displayNotificationList(ArrayList<String> notificationList) {
		Log.i(TAG, "displayNotificationList METHOD");
		// Find the ListView resource.
		Log.d(TAG, "ListView is being gotten with ID.");
		mainListView = (ListView) findViewById(R.id.mainListView);
		Log.d(TAG, "ArrayAdapter is being created with given notificationList.");
		ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
				R.layout.simple_row, notificationList);
		// Set the ArrayAdapter as the ListView's adapter.
		Log.d(TAG, "ArrayAdapter is being set as the ListView's adapter");
		mainListView.setAdapter(listAdapter);
	}

	/**
	 * This function provide us to create new Activity class when clicked list
	 * view item.
	 * 
	 * @param listView
	 */
	public void setClickListenerForListView(final ListView listView) {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.i(TAG, "onItemClick EVENT");
				// get selected item
				String notification = (String) (listView
						.getItemAtPosition(position));
				Log.d(TAG, "Clicked notification is : " + notification);
				// create new activity on list item clicked...
				Intent newActivityIntent = new Intent(
						NotificationListActivity.this,
						LocationListActivity.class);
				// sending data to new activity...
				newActivityIntent.putExtra(
						LocationListActivity.SELECTED_NOTIFICATION_PARAM_NAME,
						notification);
				// start new activity...
				startActivity(newActivityIntent);
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_hob_nob_list_notification,
				menu);
		return true;
	}

	public void processNotifications(ArrayList<String> notificationList) {
		handleRetrieveNotificationsTaskResult(notificationList);
	}

}
